package com.hyb.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hyb.Vo.StockVo;
import com.hyb.entity.Stock;
import com.hyb.mapper.StockMapper;
import com.hyb.page.PageReq;
import com.hyb.page.VoQuery;
import com.hyb.service.StockService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  库存Service接口实现类
 * </p>
 *
 * @author hyb
 * @since 2021-12-29
 */
@Service
public class StockServiceImpl extends ServiceImpl<StockMapper, Stock> implements StockService {

    @Autowired
    private StockMapper stockMapper;

    @Override
    public void createStockInfo(Stock stock) {
        stockMapper.insert(stock);
    }

    @Override
    public PageReq getStockS(Integer limit, Integer current, String productType, String cargoName) {
        Page<StockVo> page = new Page<>(current,limit);
        VoQuery voQuery = new VoQuery();
        if (!StringUtils.isEmpty(productType))
            voQuery.setProductType(productType);
        if (!StringUtils.isEmpty(cargoName))
            voQuery.setStoreName(cargoName);
        IPage<StockVo> stocks = stockMapper.getStocks(page, voQuery);
        PageReq pageReq = new PageReq();
        pageReq.setTotal(stocks.getTotal());
        pageReq.setMyData(stocks.getRecords());
        return pageReq;
    }

    @Override
    public StockVo getStockByStockId(Integer stockId) {
        return stockMapper.selectStockById(stockId);
    }

    @Override
    public void updateStock(Stock stock) {
        QueryWrapper<Stock> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("stock_id",stock.getStockId());
        stockMapper.update(stock,queryWrapper);
    }

    @Override
    public void deleteStock(Integer stockId) {
        stockMapper.deleteById(stockId);
    }

}
